package cc.rengu.igas.channel.cpcn.core.service.mchntorder;

import cc.rengu.igas.channel.cpcn.common.constant.CpcnParamConstant;
import cc.rengu.igas.channel.cpcn.common.constant.CpcnTreeNodeConstant;
import cc.rengu.igas.channel.cpcn.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.cpcn.common.util.CpcnTransferUtil;
import cc.rengu.igas.channel.cpcn.core.service.base.NoticeService;
import cc.rengu.igas.opms.facade.base.Header;
import cc.rengu.igas.opms.facade.request.CpcnResultNoticeRequest;
import cc.rengu.igas.opms.facade.response.CpcnResultNoticeResponse;
import cc.rengu.igas.opms.facade.result.Result;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.SequenceMapper;
import cc.rengu.oltp.service.common.dao.impl.SequenceMapperImpl;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * @author wukefan
 * @version 1.0.0
 * @date 2020/7/27 18:19
 */
public class CpcnMchntOrderTransResultNoticeService extends NoticeService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        CpcnResultNoticeRequest cpcnResultNoticeRequest = new CpcnResultNoticeRequest();
        ConvertUtil.convertOutput(cpcnResultNoticeRequest);
        rglog.debug("cpcnResultNoticeRequest = <{}>", cpcnResultNoticeRequest);
        return cpcnResultNoticeRequest;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        CpcnResultNoticeRequest cpcnResultNoticeRequest = (CpcnResultNoticeRequest) request;
        cpcnResultNoticeRequest.setHeader(new Header());
        cpcnResultNoticeRequest.getHeader().setInstId(CpcnTransferUtil.cpcnInstIdTransfer2CorpBank(xmlTreeUtil.getXmlTreeStringValue(CpcnTreeNodeConstant.INSTITUTION_ID)));
        cpcnResultNoticeRequest.getHeader().setChanlId(CpcnParamConstant.CHANNEL_ID_CPCN);
        cpcnResultNoticeRequest.getHeader().setSrcSysId("OPMS");
        cpcnResultNoticeRequest.getHeader().setTxnNum("T00PM063");
        cpcnResultNoticeRequest.getHeader().setTraceNo(ChannelEnum.CPCN.getChannelType().concat((xmlTreeUtil.getXmlTreeStringValue(CpcnTreeNodeConstant.INSTITUTION_ID).length() > 6) ? xmlTreeUtil.getXmlTreeStringValue(CpcnTreeNodeConstant.INSTITUTION_ID).substring(0, 6) : xmlTreeUtil.getXmlTreeStringValue(CpcnTreeNodeConstant.INSTITUTION_ID)).concat(DateUtil.getCurrentDateTime("yyyyMMddHHmmss")).concat(generateTransSeqNum()));
        cpcnResultNoticeRequest.getHeader().setBizType(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.BIZ_TYPE));
        cpcnResultNoticeRequest.setInstitutionID(xmlTreeUtil.getXmlTreeStringValue(CpcnTreeNodeConstant.INSTITUTION_ID));
        cpcnResultNoticeRequest.setOrderStatus(xmlTreeUtil.getXmlTreeStringValue(CpcnTreeNodeConstant.STATUS));
        cpcnResultNoticeRequest.setOrderAmt(AmountUtil.changeF2Y(xmlTreeUtil.getXmlTreeStringValue(CpcnTreeNodeConstant.AMOUNT)));
        DubboService dubboService = new DubboServiceImpl();
        Result<CpcnResultNoticeResponse> responseResult = (Result<CpcnResultNoticeResponse>) dubboService.callDubboService(CpcnParamConstant.DUBBO_BEAN_OPMS_PAY, CpcnParamConstant.DUBBO_METHOD_CPCN_PAY_NOTICE, cpcnResultNoticeRequest);
        if (null == responseResult || !responseResult.isSuccess() || !"00000000".equals(responseResult.getResult().getRespCode())) {
            rglog.error("支付结果通知失败");
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, RespCodeEnum.SYSTEM_ERROR.getRespCode());
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        } else {
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, RespCodeEnum.SUCCESS.getRespCode());
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, RespCodeEnum.SUCCESS.getRespDesc());
        }
        return null;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {

    }


    /**
     * 生成交易流水号--30位
     *
     * @throws Exception 业务异常
     */
    private String generateTransSeqNum() throws Exception {
        SequenceMapper sequenceMapper = new SequenceMapperImpl();
        String transSeqNo = sequenceMapper.getNextTransSequenceNo(8);
        if (null == transSeqNo) {
            rglog.error("生成交易流水序列号失败!");
            transSeqNo = "00000001";
        }
        return transSeqNo;
    }
}
